package com.studerb.view.tasks.active;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.studerb.events.NewsGroupEvent;
import com.studerb.model.NewsGroup;
import com.studerb.service.EventService;
import com.studerb.service.interfaces.INewsGroupService;
import com.studerb.service.interfaces.ITaskService;
import com.studerb.tasks.TaskType;
import com.studerb.tasks.interfaces.ITask;
import com.studerb.tasks.interfaces.ITaskFactory;

public class ActiveTasksPane extends JPanel implements ActionListener {
	@Autowired protected ActiveTasksTablePane activeTasksTablePane;
	@Autowired protected ITaskService taskService;
	@Autowired protected ITaskFactory taskFactory;
	@Autowired protected INewsGroupService newsGroupService;
	@Autowired protected EventService eventService;
	protected JPanel buttonPane;
	protected JButton taskButton1, taskButton2, taskButton3, taskButton4;

	@Override public void actionPerformed(ActionEvent e) {
		JButton clickedButton = (JButton) e.getSource();
		ITask task = null;
		if (clickedButton == taskButton1) {
			task = taskFactory.getTask(TaskType.DUMMY);
		}
		else if (clickedButton == taskButton2) {
			task = taskFactory.getTask(TaskType.HELP);
		}
		else if (clickedButton == taskButton3) {
			task = taskFactory.getTask(TaskType.FAILING);
		}
		else if (clickedButton == taskButton4) {
			NewsGroup newsGroup = new NewsGroup(RandomStringUtils.randomAlphabetic(30), 20, true);
			NewsGroupEvent ngEvent = new NewsGroupEvent(NewsGroupEvent.EventType.HEADERS, newsGroup);
			eventService.publish(ngEvent);
		}

		if (task != null) {
			taskService.addTask(task);
		}
	}

	public void init() {
		this.setLayout(new BorderLayout());
		buttonPane = new JPanel();
		/*
		 * taskButton1 = new JButton("Dummy Task"); taskButton2 = new
		 * JButton("HelpCommand Task"); taskButton3 = new
		 * JButton("Failing Task"); taskButton4 = new JButton("NewsGroupTask");
		 * 
		 * buttonPane.add(taskButton1); buttonPane.add(taskButton2);
		 * buttonPane.add(taskButton3); buttonPane.add(taskButton4);
		 * 
		 * taskButton1.addActionListener(this);
		 * taskButton2.addActionListener(this);
		 * taskButton3.addActionListener(this);
		 * taskButton4.addActionListener(this);
		 */
		add(buttonPane, BorderLayout.NORTH);
		add(activeTasksTablePane, BorderLayout.CENTER);
	}
}
